package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 山顶洞人郑某
 * @className ShoppingCartController.java
 * @date 2024 02 08
 */
@RequestMapping("/user/shoppingCart")
@Slf4j
@RestController
@Api(tags = "购物车管理")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "购物车查询")
    public Result<List<ShoppingCart>> list(){
       List<ShoppingCart> list = shoppingCartService.list();
       log.info("购物车信息{}",list);
       return Result.success(list);
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加的购物车信息为{}",shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success("添加成功");
    }
    @ApiOperation(value = "减去购物车中的一个商品")
    @PostMapping("/sub")
    public Result<String > sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
      log.info("删除购物车中的一个商品信息为{}",shoppingCartDTO);
      shoppingCartService.sub(shoppingCartDTO);
      return Result.success();
    }

    @ApiOperation(value = "清空购物车")
    @DeleteMapping("/clean")
    public Result<String> clean(){
      log.info("清空购物车");
      shoppingCartService.clean();
      return Result.success();
    }
}
